package com.oa.core.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.oa.admin.entity.Holiday;
import com.oa.admin.entity.UserHoliday;
import com.oa.admin.service.IHolidayService;
import com.oa.admin.service.IUserHolidayService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class HolidayTask {

    @Autowired
    private IHolidayService holidayService;

    @Autowired
    private IUserHolidayService userHolidayService;

    @Scheduled(cron = "0 0 1 1 * ? ")
    public void monthUpdate() {
        String holidayNamesStr = updateHoliDay(1);
    }

    @Scheduled(cron = "0 59 23 31 12 ?")
    public void yearUpdate() {
        String holidayNamesStr = updateHoliDay(2);


    }

    private String updateHoliDay(Integer dateType){
        List<Holiday> holidays = holidayService.selectList(new EntityWrapper().eq("date_type", 2));

        List<String> holidayNames = new ArrayList<>();

        holidays.forEach(d -> {
            UserHoliday userHoliday = new UserHoliday();
            userHoliday.setAlreadyUsedDay(0.0);
            userHoliday.setSurplusDay(d.getHolidayNumber());

            holidayNames.add(d.getHolidayName());

            userHolidayService.update(userHoliday, new EntityWrapper().eq("holiday_id", d.getId()));
        });

        return StringUtils.join(holidayNames, ",");
    }
}
